package com.example.freshmenclient.fragments;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.example.freshmenclient.R;
import com.example.freshmenclient.adapter.LeaveWordAdapter;
import com.example.freshmenclient.bean.LeavewordsBean;
import com.example.freshmenclient.util.GainDataUtil;
import com.example.freshmenclient.util.UrlUtil;
import com.google.gson.Gson;

import org.json.JSONObject;


public class LeaveWordFragment extends Fragment {
    private static final String TAG = "LeaveWordFragment";

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ListView lvLeaveword;
    private LeavewordsBean leavewordsBean;


    public LeaveWordFragment() {
        // Required empty public constructor
    }


    public static LeaveWordFragment newInstance(String param1, String param2) {
        LeaveWordFragment fragment = new LeaveWordFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_leave_word, container, false);
        lvLeaveword = view.findViewById(R.id.lv_leaveword);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String url = UrlUtil.basePath + "freshmenserver/messages/list";
        GainDataUtil.gainDataForPost(getContext(), url, null
                , new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG, "onResponse: " + jsonObject);
                        Gson gson = new Gson();
                        leavewordsBean = gson.fromJson(jsonObject.toString(), LeavewordsBean.class);
                        lvLeaveword.setAdapter(new LeaveWordAdapter(getContext(), leavewordsBean.getData()));
                    }
                }, null);

    }
}
